import scrapy
from demo2.items import dangdangItem
#scrapy crawl dangdang -o dangdang.csv
class DangdangSpider(scrapy.Spider):
    name = "dangdang"
    allowed_domains = ["dangdang.com"]
    start_urls = ["http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1"]

    def parse(self, response):
        open(f"dangdang{1}.html", "wb").write(response.body)
        list_items = response.css('body > div.bang_wrapper > div.bang_content > div.bang_list_box > ul > li')
        for book in list_items:
            item = dangdangItem()
            # 提取书名
            item['book_name'] = book.css('.name a::attr(title)').get()

            # 提取作者
            authors = book.css('.publisher_info a::text').getall()
            # 过滤掉非作者信息（如出品方）
            valid_authors = [author for author in authors if '出品' not in author]
            item['authors'] = ', '.join(valid_authors)

            # 提取出版社
            publisher = book.css('div:nth-child(6) a::text').get()
            if publisher:
                publisher = publisher.replace('出版社', '').strip()
            item['publisher'] = publisher

            # 提取出版日期
            item['publish_date'] = book.css('.publisher_info span::text').get()

            # 提取售价
            sell_price = book.css('.price_n::text').get()
            if sell_price:
                item['sell_price'] = sell_price.strip('¥')

            # 提取定价
            define_price = book.css('.price_r::text').get()
            if define_price:
                item['define_price'] = define_price.strip('¥')

            # 提取折扣
            discount = book.css('.price_s::text').get()
            if discount:
                item['discount'] = discount.strip('折')

            # 提取评论数量
            comment_text = book.css('.star a::text').get()
            if comment_text:
                item['comments'] = ''.join(filter(str.isdigit, comment_text))

            # 提取推荐百分比
            recommend_text = book.css('.star .tuijian::text').get()
            if recommend_text:
                item['recommend_percentage'] = recommend_text.replace('推荐', '')

            yield item
        # list_items = response.css('body > div.bang_wrapper > div.bang_content > div.bang_list_box > ul > li')
        # for book in books:
        #     item = jingdongItem()
        #     item['sequence'] = book.get('sequence')
        #     item['discount'] = book.get('discount')
        #     item['publisher'] = book.get('publisher')
        #     item['sellPrice'] = book.get('sellPrice')
        #     item['definePrice'] = book.get('definePrice')
        #     item['bookName'] = book.get('bookName')
        #     item['authors'] = book.get('authors')[0].get('name')
        #     item['bookId'] = book.get('bookId')
        #     yield item
        pass
